﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using ROrganizer.Client;

namespace ROrganizer.UI.Controllers
{
    internal class CreateUserFormController
    {
        private Image image;
        private HashCaptcha captcha;

        public CreateUserFormController()
        {
            var gateway = new HashGateway();

            this.captcha = gateway.GetCaptcha();
            using (var stream = new MemoryStream(captcha.Image))
                this.image = Bitmap.FromStream(stream);
        }

        public string Username { get; set; }
        public string Password { get; set; }
        public string Retyped { get; set; }
        public string Captcha { get; set; }

        public Image Image
        {
            get { return this.image; }
        }

        public void Refresh()
        {
            var gateway = new HashGateway();
            this.captcha = gateway.GetCaptcha();

            using (var stream = new MemoryStream(captcha.Image))
                this.image = Bitmap.FromStream(stream);
        }

        public bool CanCreate()
        {
            return !String.IsNullOrWhiteSpace(this.Username) && this.Username.Trim().Length >= 5
                && !String.IsNullOrEmpty(this.Password) && this.Password == this.Retyped
                && !String.IsNullOrWhiteSpace(this.Captcha);
        }

        public bool Create()
        {
            var gateway = new HashGateway();
            var result = gateway.CreateUser(this.captcha, this.Username, this.Password, this.Captcha);

            return result;
        }
    }
}
